﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using MediaMonitor.Common;
using Adex;

namespace MediaMonitor.admin
{
    public partial class news_template : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            { 
                if(Request["tid"]!=null && Request["tid"] !="")
                {
                    NewsTemplate nt = new NewsTemplate(Request["tid"].ToString());
                    tbName.Text = nt.TemplateName;
                } 
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {


            string errMsg1 = "", errMsg2 = "", errMsg3 = "";
            
            string file1 = "", file2 = "", file3="";


            if (flPicture1.PostedFile.FileName.Length > 0)
            {
                file1 = UploadTemplatePciture(flPicture1, "1", out errMsg1);
                lblMsg1.ForeColor = file1.Length>0 ? Color.Blue : Color.Red;
                lblMsg1.Text = "Picture 1:" + errMsg1;
            }
            else
                lblMsg1.Text = "Picture 1:" + "No file selected to upload";


            if (flPicture2.PostedFile.FileName.Length > 0)
            {
                file2 = UploadTemplatePciture(flPicture2, "2", out errMsg1);
                lblMsg2.ForeColor = file2.Length>0 ? Color.Blue : Color.Red;
                lblMsg2.Text = "Picture 2:" + errMsg1;
            }
            else
                lblMsg2.Text = "Picture 1:" + "No file selected to upload";

            if (flPicture3.PostedFile.FileName.Length > 0)
            {
                file3 = UploadTemplatePciture(flPicture3, "3", out errMsg1);
                lblMsg3.ForeColor =  file3.Length>0 ? Color.Blue : Color.Red;
                lblMsg3.Text = "Picture 3:" + errMsg1;
            }
            else
                lblMsg3.Text = "Picture 3:" + "No file selected to upload";


            Adex.NewsTemplate tmpl = new NewsTemplate();

            if(Request["tid"]!=null && Request["tid"]!="")
            {
                tmpl = new Adex.NewsTemplate(Request["tid"]);
            }

            
            tmpl.TemplateName = tbName.Text.Trim();
            tmpl.Picture1 = file1.Length>0 ? file1 : tmpl.Picture1;
            tmpl.Picture2 = file2.Length > 0 ? file2 : tmpl.Picture2;
            tmpl.Picture3 = file3.Length > 0 ? file3 : tmpl.Picture3;

            tmpl.Save();

            if (tmpl.TemplateID > 0)
            {

                lblMsg1.Text = "<h3>Tempate Saved Successfully.</h3><br><br>" + lblMsg1.Text;
            }
            else
            {
                lblMsg1.Text = "<h3>Tempate not saved</h3><br><br>" + lblMsg1.Text;
            }
            pnlUpload.Visible = false;
                pnlInfo.Visible = true;
             
        }

        private string UploadTemplatePciture(FileUpload flPicture1, string picType, out string errMsg)
        {
            try
            {
                string fileName = SubSonic.Sugar.Strings.ToProper(tbName.Text.Trim().Replace(" ", "")) + "_" + picType;
                string retFileName = "";
               retFileName = Common.Utility.UploadImage(flPicture1, "../images/templates/", fileName, retFileName, false, 0, 0, 10, out errMsg);

                return retFileName;
            }
            catch (Exception ex)
            {
                errMsg = "Failed to upload image " + ex.Message;
            }

            return "";
         }

        protected void lnkAddNew_Click(object sender, EventArgs e)
        {
            pnlInfo.Visible = false;
            pnlUpload.Visible = true;

            tbName.Text = "";

        }
    }
}